package cn.lyh97.estore.easystoremanager.Classes;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.lyh97.estore.easystoremanager.Activities.MainActivity;
import cn.lyh97.estore.easystoremanager.R;

import java.util.List;

public class GoodsAdapter extends BaseAdapter {

    private List<GoodsItem> _list;
    public LayoutInflater _inflater;

    public GoodsAdapter(Context context, List<GoodsItem> list){
        _list = list;
        _inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return _list.size();
    }

    @Override
    public Object getItem(int i) {
        return _list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view == null){
            viewHolder = new ViewHolder();
            view = _inflater.inflate(R.layout.goods_item,null);
            viewHolder._itemID = (TextView) view.findViewById(R.id.goodsItemIDTxt);
            viewHolder._image = (ImageView) view.findViewById(R.id.goodsItemImg);
            viewHolder._name = (TextView) view.findViewById(R.id.goodsItemNameTxt);
            viewHolder._num = (TextView) view.findViewById(R.id.goodsItemNumTxt);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        GoodsItem goodsItem = _list.get(i);
        viewHolder._itemID.setText(goodsItem.getID());
        viewHolder._image.setImageResource(Integer.parseInt(goodsItem.getImgID()) );
        viewHolder._name.setText(goodsItem.getName());
        viewHolder._num.setText(goodsItem.getNum().toString());
        return view;
    }

    private class ViewHolder {
        public TextView _itemID;
        public ImageView _image;
        public TextView _name;
        public TextView _num;
    }
}

